/*
    处理留言数据的逻辑
*/
const { sqlQuery } = require('../mysql/index')

module.exports = {
    getDepatureList, addDepature, likeDepature, getCommentList, addComment
}

//获取所有留言列表
async function getDepatureList() {
    let sql = `select * from depature_data ORDER BY time desc;`
    return new Promise(function (resolve, reject) {
        sqlQuery(sql, (data) => {
            for (let i in data) {
                data[i].isName = data[i].isName == 1 ? true : false;
                data[i].isImg = data[i].isImg == 1 ? true : false;
                data[i].isPublic = data[i].isPublic == 1 ? true : false;
                data[i].name = specialName(data[i].name)
                if (data[i].name == '') {
                    data[i].name = null
                }
                if (i == data.length - 1) {
                    resolve(data)
                }
            }
        })
    })
}

//新增留言
async function addDepature(data) {
    let content = data.content;
    let name = data.name;
    let time = data.time;
    let isName = (data.isName == true || data.isName == 'true') ? 1 : 0;
    let isImg = (data.isImg == true || data.isImg == 'true') ? 1 : 0;
    let isPublic = (data.isPublic == true || data.isPublic == 'true') ? 1 : 0;
    let _like = data._like;
    let head = data.head;
    let sql = `insert into depature_data(content,time,name,isName,isImg,isPublic,_like,head) values("${content}","${time}","${isName == 1 ? name : ''}","${isName}","${isImg}","${isPublic}","${_like}","${head}");`
    return new Promise(function (resolve, reject) {
        sqlQuery(sql, data => {
            resolve(data)
        })
    })
}

//点赞留言
async function likeDepature(id) {
    let sql = `update depature_data set _like=_like+1 where id=${id};`
    return new Promise(function (resolve, reject) {
        sqlQuery(sql, (data) => {
            resolve(data)
        })
    })
}

//评论留言
async function addComment(msg) {
    let d_id = msg.d_id;
    let name = msg.name;
    let time = msg.time;
    let content = msg.content;

    let sql = `insert into d_comment(d_id,name,time,content) values("${d_id}","${name}","${time}","${content}");`
    return new Promise(function (resolve, reject) {
        sqlQuery(sql, (data) => {
            resolve(data)
        })
    })
}

//获取留言评论列表
async function getCommentList(d_id) {
    let sql = `select * from d_comment where d_id=${d_id} ORDER BY time;`
    return new Promise(function (resolve, reject) {
        sqlQuery(sql, (data) => {
            resolve(data)
        })
    })
}

//特殊名字特殊操作
/* 
    对应表：
    徐海吟，XHY，xhy，囧侠客，小徐小徐骑驴找驴 -- 小徐小徐骑驴找驴
    陆培儿，lpe，LPE -- 伟大的母亲
    刘晓倩，LXQ，lxq，笑，Xiao^Xiao，XiaoXiao，xq -- 晓倩
*/
function specialName(name) {
    if (name == '徐海吟' || name == 'XHY'
        || name == 'xhy' || name == '囧侠客'
        || name == '小徐小徐骑驴找驴') {
        return '小徐小徐骑驴找驴'
    }
    if (name == '刘晓倩' || name == 'LXQ'
        || name == 'lxq' || name == '笑'
        || name == 'Xiao^Xiao' || name == 'XiaoXiao'
        || name == 'xq') {
        return '晓倩'
    }
    if (name == '陆培儿' || name == 'LPE' || name == 'lpe') {
        return '伟大的母亲'
    }
    return name
}